import router from './router'
import store from './store'

// 路由守卫
const whiteList = ['/login', '/404']
router.beforeEach(async(to, from, next) => {
  const { token, userId } = store.getters
  // console.log(token)
  if (token) {
    if (!userId) {
      // 当前如果有用户id的时候,代表已经存储用户信息了,不需要重复获取,只有在没有用户信息的时候,才需要去获取,等待信息获取成功之后再跳转页面
      await store.dispatch('user/getUserInfo')
    }
    if (to.path !== '/login') {
      next()
    } else {
      next('/dashboard')
    }
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
